package com.person.mengshuai.mytestdemo.service.impl;

import cn.dev33.satoken.secure.SaSecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.person.mengshuai.mytestdemo.domain.response.Result;
import com.person.mengshuai.mytestdemo.entity.BUser;
import com.person.mengshuai.mytestdemo.mapper.BUserMapper;
import com.person.mengshuai.mytestdemo.service.BUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 系统管理-用户基础信息表 服务实现类
 * </p>
 *
 * @author ms
 * @since 2024-08-02
 */
@Service
public class BUserImpl extends ServiceImpl<BUserMapper, BUser> implements BUserService {

    @Override
    public Result insertUser(BUser bUser) {
        String username = bUser.getUsername();
        bUser.setPassword(SaSecureUtil.aesEncrypt("password", bUser.getPassword()));
        BUser bUser1 = findUserByUsername(username);
        if (bUser1 != null) {
            return Result.failure("当前用户名已存在，请使用新的用户名");
        }
        return Result.success(baseMapper.insert(bUser));
    }

    @Override
    public BUser findUserByUsername(String username) {
        QueryWrapper<BUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BUser::getUsername, username);
        return baseMapper.selectOne(queryWrapper);
    }
}
